﻿var jsonNav = category;
//如果网站定义在子目录，需修改此处，eg:/blog/
var urlPath = WebLang.Lang == currentLang ? "/" : "/" + WebLang.Lang + "/";
//无限极菜单
function ViewNav(parentid, layer) {
    var layer = layer + 1;
    var navItemStr;
    var currentParent;
    var currentItem;

    if (parentid == 0) {
        currentParent = $(".menu");
        currentParent.append("<ul class=\"topnav\"><li id=\"Nav_0\" v=\"0\"><a href=\"" + urlPath + "\">" + WebLang.Home + "</a></li></ul>");
    }
    else {
        currentParent = $("#Nav_" + parentid);
        if (layer == 2)
        { currentParent.append("<ul class=\"subnav\" id=\"PNav_" + parentid + "\"></ul>"); }
        else
        { currentParent.append("<ul class=\"subnav2\" id=\"PNav_" + parentid + "\"></ul>"); }
    }
    currentParent = currentParent.find("ul");

    //循环取json中的数据
    $.each(jsonNav, function (i) {

        if ((jsonNav[i].IsNav == 1) && (jsonNav[i].ParentId == parentid)) {
            //添加
            var urlstr = urlPath+"cate/" + jsonNav[i].CateId;
            if (jsonNav[i].ReName != "")
            { urlstr = urlPath + jsonNav[i].ReName+"/"; }
            navItemStr = "<li id=\"Nav_" + jsonNav[i].CateId + "\" v=\"" + jsonNav[i].CateId + "\"><a href=\"" + urlstr + "\">" + GetCateName(jsonNav[i].CateId, jsonNav[i].CateName) + "</a></li>";
            currentParent.append(navItemStr)
            currentItem = $("#Nav_" + jsonNav[i].CateId);

            //有子栏目时添加下拉样式标签以及hasSub样式（用于显示子栏目的操作）
            if (jsonNav[i].SubCount > 0 && jsonNav[i].Type != "5") {
                if (layer == 1)
                { currentItem.append("<span></span>"); }
                else {
                    currentItem.find("a").css({ "background-position": "145px 15px" });
                }
                currentItem.addClass("hasSub");
                ViewNav(jsonNav[i].CateId, layer);
            }

            if (layer > 2)
            { currentItem.find("a").css({ "float": "left", "width": "150px" }); }
        }
    });
}

//根据cateid查询catename
function GetCateName(cateid, catename) {
    if (WebLang.Lang == "") { return catename; }
    var t = "";
    $.each(categorylang, function (i) {
        if (categorylang[i].CateId == cateid) { t = categorylang[i].CateName; return; }

    });
    if (t == "") { t = catename; }
    return t;
}

//取得当前时间
function GetCurrentTime() {
    var date = new Date();
    var now = "";
    now = date.getFullYear() + "-"; 
    now = now + (date.getMonth() + 1) + "-"; 
    now = now + date.getDate() + " ";
    now = now + date.getHours() + ":";
    now = now + date.getMinutes() + ":";
    now = now + date.getSeconds();
    return now;
}

//文章列表修饰图异步加载
//function LoadSummaryImg() {
//    if (summaryimgs) {
//        $.each(summaryimgs, function (i, n) {
//            $("#simg_" + n["id"]).html("<img src=\"" + n["img"] + "\"/>");
//        });
//    }
//}

//评论提交成功后执行
function CommentSubmitSuccess(re) {
    if (re == 0) {
        var userinfo = $('#UserName').val();
        if ($('#Url').val().replace(/\s/g, "") != "") {
            userinfo = "<a href=\"" + $('#Url').val() + "\" target=\"_blank\">" + $('#UserName').val() + "</a>"
        }
        $(".commentlist ul").append("<li><p>" + userinfo + " " + GetCurrentTime() + "</p>" + $("#Reply").val() + "</li>");

        $("#Reply").val('');

        $('#AjaxResult').show().html(WebLang.AjaxSuccess).hide(3000);
    }
    else {
        $('#AjaxResult').show().html(WebLang.AjaxFaild).hide(3000);
    }
}

//异步读取评论
function GetComment(pageno) {
    $('#comment').html('loading...');
        $.get(urlPath + "home/CommentList/", { id: ArticleId, pageNo: pageno, rn: Math.random() }, function (re) {
        $("#comment").html(re);
    })
}

//加载日历
function initArticleCalendar() {
    var datepickerYear = $(".ui-datepicker-year").html();

    //英文月份：alert($.datepicker.regional[''].monthNames);
    var datepickerMonth;
    if (WebLang.Lang == "" || WebLang.Lang == "zh-tw" || WebLang.Lang == "zh-cn") {
        datepickerMonth = $.inArray($(".ui-datepicker-month").html(), $.datepicker.regional['zh-CN'].monthNames) + 1;
    }
    if (WebLang.Lang == "en-us") {
        datepickerMonth = $.inArray($(".ui-datepicker-month").html(), $.datepicker.regional[''].monthNames) + 1;
    }

    $(".ui-datepicker-calendar a").each(function (index, domEle) {

        var datepickerDay = $(this).html();
        var datepickerDate = datepickerYear + "-" + datepickerMonth + "-" + datepickerDay;

        if ($.inArray(datepickerDate, strArticleDates) > -1) {
            $(this).parent().attr("onclick", "null");
            $(this).attr("href", urlPath + "Archives/" + datepickerDate + "/").css({ "background": "#680707" });
        }
        else {
            $(this).parent().attr("onclick", "function(){return false;}").html("<div class=\"ui-state-word\">" + datepickerDay + "</div>");
        }
    });
}


$(function () {
    //导航显示子栏目
    $(".hasSub").hover(function () {
        $(this).find("ul").first().show();
        $(this).addClass("subhover");
    }, function () {
        $(this).children("ul").hide();
        $(this).removeClass("subhover");
    });

    //登录信息
    //    $.get(urlPath + "Account/CheckLogin/", { rn: Math.random() }, function (re) {
    //        $("#logindisplay").html(re);
    //    })

    //推荐
    $(".digg_favor").click(function () {
        $.ajax({
            url: urlPath + "home/AjaxVote/",
            type: "post",
            data: { articleid: ArticleId, vote: 1 },
            dataType: 'json',
            success: function (re) {
                $(".digg_tip").html(re["message"]);
                if (re["error"] == 0)
                { $(".digg_favor").html(re["value"]); }
            }
        });
    });

    //反对
    $(".digg_against").click(function () {
        $.ajax({
            url: urlPath + "home/AjaxVote/",
            type: "post",
            data: { articleid: ArticleId, vote: 0 },
            dataType: 'json',
            success: function (re) {
                $(".digg_tip").html(re["message"]);
                if (re["error"] == 0)
                { $(".digg_against").html(re["value"]); }
            }
        });
    });

    //评论翻页
    $(".commentpager a").live("click", function () {
        GetComment($(this).attr("p"));
    });


    //引用
    $(".reply-quote").live("click", function () {
        var curl = window.location.href;
        curl = curl.substr(0, curl.indexOf('#'));
        window.location.href = "#commentform";
        var node = $(this).parent().parent();
        var replyid = node.attr("id");
        var uname = node.attr("u");
        var replycontent = node.find(".replycontent").html();
        var txt = $("#Reply").val();
        //txt = txt + "<fieldset class=\"comment_quote\"><legend><a title=\"" + WebLang.ViewContent + "\" href=\"" + curl + "#" + replyid + "\">" + WebLang.Quote + "</a></legend>" + uname + "：<br>" + replycontent + "</fieldset>";
        txt = txt + "[quote]" + uname + "：\r\n" + replycontent + "[/quote]\r\n";
        $("#Reply").focus().val(txt);

        //nEditor.tinsertUBB(txt);
        //nEditor.setValueHTML(txt);
    });

    //回复
    $(".reply-re").live("click", function () {
        window.location.href = "#commentform";
        var node = $(this).parent().parent();
        var uname = node.attr("u");
        var txt = $("#Reply").val();
        txt = "@" + uname + "\r\n" + txt;
        $("#Reply").focus().val(txt);
    });

    //搜索
    $(".search_button").click(function () {
        window.location.href = urlPath + "search/" + $("#searchkey").val().replace(/\./g, "");
    });

    //语言/风格切换
    $(".dropmenu").live({
        mouseenter:
        function () {
            $(this).find(".dropmenu-list").show();
            $(this).find(".dropmenu-title").addClass("dropmenutitlehover");
        },
        mouseleave:
        function () {
            $(this).find(".dropmenu-list").hide();
            $(this).find(".dropmenu-title").removeClass("dropmenutitlehover");
        }
    });
    $(".dropmenu-list a").live({
        mouseenter:
        function () {
            $(this).addClass("langlihover");
        },
        mouseleave:
        function () {
            $(this).removeClass("langlihover");
        }
    });

    //不同模板下图片路径
    var currentThemePath = "/Content/image/";

    if (currentTheme != "") {
        currentThemePath = "/Themes/" + currentTheme + "/Style/image/";
    }

    //装饰图延迟加载
    $(".post_image img").lazyload({
        placeholder: currentThemePath + "decorate/default.jpg",
        effect: "fadeIn"
    });


});



